//
//  MyAFNetworking.m
//  ZWXanfansystem
//
//  Created by zzp on 15/6/3.
//  Copyright (c) 2015年 zzp. All rights reserved.
//

#import "MyAFNetworking.h"

//#define BaseUrl @"221.228.242.122:5001"

#define APP_URL @""

#define TimeOut 3
#define KUser @""
#define KService @"jkzQueryService"

@implementation MyAFNetworking

+(NSString *)GetWebUrl:(NSString *)urlStr{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    NSString *webUrl=@"";
    NSString *url=@"";
    
    if ([defaults objectForKey:@"webUrl"]) {
        webUrl=[defaults objectForKey:@"webUrl"];
    }
    else{
        webUrl= [AppInfo sharedInstance].baseURL;
    }
    url=[NSString stringWithFormat:@"http://%@/%@/%@",webUrl,KService,urlStr];
    
    return url;
}

+(AFHTTPSessionManager *)GetManagerWithTypes:(NSString *)type{
    
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObject:type];
    manager.requestSerializer.timeoutInterval = TimeOut;
    
    return manager;
}

+(void)Post:(NSString *)urlStr addParameters:(NSDictionary *)parameters andSucessBlock:(AFHTTPRequestSucess)sucess andFailureBlock:(AFHTTPRequestFailure)failure{
    
    NSString *url = [self GetWebUrl:urlStr];
    
    AFHTTPSessionManager *manager = [self GetManagerWithTypes:@"text/html"];

    [manager POST:url parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        sucess(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        failure(task,error);
    }];
}

+(void)Get:(NSString *)urlStr andSucessBlock:(AFHTTPRequestSucess)sucess andFailureBlock:(AFHTTPRequestFailure)failure{
    
    NSString *url = [self GetWebUrl:urlStr];
    
    AFHTTPSessionManager *manager = [self GetManagerWithTypes:@"text/html"];
    
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        sucess(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        failure(task,error);
    }];
}

+(void)PostImage:(NSString *)urlStr addParameters:(NSDictionary *)parameters andData:(NSData *)data andSucessBlock:(AFHTTPRequestSucess)sucess andFailureBlock:(AFHTTPRequestFailure)failure{
    
    NSString *url = [self GetWebUrl:urlStr];
    
    AFHTTPSessionManager *manager = [self GetManagerWithTypes:@"text/html"];
    
    [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        
        NSString *fileName = [NSString stringWithFormat:@"%@%@.jpg",KUser,str];
        
        [formData appendPartWithFileData:data name:@"imageFile" fileName:fileName mimeType:@"image/jpg"];
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        sucess(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        failure(task,error);
    }];
}

+(void)testUrl:(NSString *)weburl andSucessBlock:(AFHTTPRequestSucess)sucess andFailureBlock:(AFHTTPRequestFailure)failure{

    AFHTTPSessionManager *manager = [self GetManagerWithTypes:@"text/html"];
    
    [manager POST:weburl parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        sucess(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        failure(task,error);
    }];
}

+(void)Updata{
    
    AFHTTPSessionManager *manager = [self GetManagerWithTypes:@"text/javascript"];
    
    [manager GET:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APP_URL] parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSString *newVersion=@"0";
        NSString *newURL=@"";
        
        NSArray *configData = [responseObject valueForKey:@"results"];
        for(id config in configData)
        {
            newVersion = [config valueForKey:@"version"];
            //获取应用程序的地址
            newURL = [config objectForKey:@"trackViewUrl"];
        }
        
        //取得本地程序的版本号
        NSDictionary *localDic =[[NSBundle mainBundle] infoDictionary];
        NSString *localVersion =[localDic objectForKey:@"CFBundleShortVersionString"];
        
        if ([newVersion floatValue]>[localVersion floatValue]) {
 
            [self performSelector:@selector(openUrl:) withObject: newURL afterDelay:1.0];
            /*
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }];
            
            UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                [self performSelector:@selector(openUrl:) withObject: newURL afterDelay:1.0];
            }];
            
            [alertController addAction:cancelAction];
            [alertController addAction:destructiveAction];
            
            [AppDelegate presentViewController:alertController animated:YES completion:nil];
             */
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"%@",error);
    }];
}

+(void)openUrl:(NSString *)url{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

@end
